package com.zh.controller;


import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.zh.Exception.AddressBookNONE;
import com.zh.Exception.ShoppingCartNONE;
import com.zh.common.BaseContext;
import com.zh.common.Result;
import com.zh.dto.OrdersDto;
import com.zh.entity.OrderDetail;
import com.zh.entity.Orders;
import com.zh.service.OrderDetailService;
import com.zh.service.OrdersService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.format.annotation.DateTimeFormat;
import org.springframework.web.bind.annotation.*;

import java.time.LocalDateTime;
import java.util.ArrayList;
import java.util.List;
import java.util.stream.Collectors;

/**
 * <p>
 * 订单表 前端控制器
 * </p>
 *
 * @author 你爹
 * @since 2023-09-21
 */
@RestController
@RequestMapping("/order")
@Slf4j
public class OrdersController {

    @Autowired
    OrdersService ordersService;
    @Autowired
    OrderDetailService orderDetailService;

    @PostMapping("/submit")
    public Result submit(@RequestBody Orders orders) throws ShoppingCartNONE, AddressBookNONE {
        log.info("orders:{}", orders);
        ordersService.submit(orders);
        return Result.success();
    }

    @PutMapping
    public Result setStatus(@RequestBody Orders orders) {
        log.info("orders:{}", orders);
        ordersService.setStatus(orders);
        return Result.success();
    }

    @GetMapping("/userPage")
    public Result<Page<OrdersDto>> userPage(Integer page, Integer pageSize) {
        log.info("分页查询：page:{},pageSize:{}", page, pageSize);
        Page<Orders> p = new Page<>(page, pageSize);
        LambdaQueryWrapper<Orders> wrapper = new LambdaQueryWrapper<>();
        wrapper.eq(Orders::getUserId, BaseContext.getCurrentId());
        wrapper.orderByDesc(Orders::getCheckoutTime).orderByDesc(Orders::getOrderTime);
        ordersService.page(p, wrapper);
        //构建Page<OrdersDto>
        Page<OrdersDto> pageInfo = new Page<>();
        List<OrdersDto> ordersDtoList = new ArrayList<>();
        BeanUtils.copyProperties(p, pageInfo, "records");
        p.getRecords().forEach(orders -> {
            OrdersDto ordersDto = new OrdersDto();
            BeanUtils.copyProperties(orders, ordersDto);
            //设置每条订单的订单明细
            LambdaQueryWrapper<OrderDetail> wrapper1 = new LambdaQueryWrapper<>();
            wrapper1.eq(OrderDetail::getOrderId, orders.getNumber());
            List<OrderDetail> orderDetailList = orderDetailService.list(wrapper1);
            ordersDto.setOrderDetails(orderDetailList);
            //设置每个订单总餐数
            Integer sumNum = orderDetailList.stream().mapToInt(OrderDetail::getNumber).sum();
            ordersDto.setSumNum(sumNum);
            ordersDtoList.add(ordersDto);
        });
        pageInfo.setRecords(ordersDtoList);
        return Result.success(pageInfo);
    }

    @GetMapping("/page")
    public Result<Page<Orders>> page(Integer page, Integer pageSize, String number, @DateTimeFormat(pattern = "yyyy-MM-dd HH:mm:ss") LocalDateTime beginTime, @DateTimeFormat(pattern = "yyyy-MM-dd HH:mm:ss") LocalDateTime endTime) {
        log.info("后台查询orders");
        Page<Orders> p = new Page<>(page, pageSize);
        LambdaQueryWrapper<Orders> wrapper = new LambdaQueryWrapper<>();
        wrapper.likeRight(number != null, Orders::getNumber, number);
        wrapper.gt(beginTime != null, Orders::getOrderTime, beginTime);
        wrapper.lt(endTime != null, Orders::getOrderTime, endTime);
        ordersService.page(p, wrapper);
        return Result.success(p);
    }
}

